﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using CE.User;
using CE.Global;
using CE.Provider;

public partial class Provider_CarInsurance_ProviderCarBaseValue : System.Web.UI.Page
{
    public static Int64 _baseid = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] == null || Session[CE.Global.General.ProviderCompanyId] == null)
        {
            Response.Redirect("~/Provider/ProviderSecureLogin.aspx");
        }
        if (!IsPostBack)
        {
            ShowCarBaseValue();
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {            
            string _authcode = CE.Security.MySecurity.Encrypt(txtAuthCode.Text, true);
            if (CheckAuthCode(_authcode, 100))
            {
                CarBaseValue _carbasevalue = new CarBaseValue();
                CarBaseValueDB _carbasevaluedb = new CarBaseValueDB();

                _carbasevalue.BaseId = _baseid;
                _carbasevalue.ProviderCompanyId = Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId].ToString());
                _carbasevalue.BaseValue = Convert.ToInt32(txtBasevalue.Text.Trim());
                _carbasevalue.RefCEUserId = Convert.ToInt64(Session[CE.Global.General.UserId].ToString());
                if (chkStatus.Checked == true)
                {
                    _carbasevalue.Status = true;
                }
                else if (chkStatus.Checked == false)
                {
                    _carbasevalue.Status = false;
                }


                //_carbasevalue.BaseId = _carbasevaluedb.Insert(_carbasevalue);

                if (_carbasevalue.BaseId > 0)
                {

                    lblMessage.Text = "Saved successfully!";
                }
                else
                {
                    lblError.Text = "Saving failed!";
                }
            }
            else
            {
                lblMessage.Text = "Wrong auth code!";
            }
        }
    }

    protected void btnClearForm_Click(object sender, EventArgs e)
    {
        lblError.Text = "";
        lblMessage.Text = "";
    }

    protected void ibtBack_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("TableMatching.aspx");
    }

    private void ShowCarBaseValue()
    {
        CarBaseValue _carbasevalue = new CarBaseValue();
        CarBaseValueDB _carbasevaluedb = new CarBaseValueDB();
        //_carbasevalue = _carbasevaluedb.GetCarBaseValue(Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId].ToString()));
        if (_carbasevalue != null)
        {
            _baseid = Convert.ToInt64(_carbasevalue.BaseId.ToString());
            txtBasevalue.Text = _carbasevalue.BaseValue.ToString();
            if (_carbasevalue.Status == true)
            {
                chkStatus.Checked = true;
            }
            else
            {
                chkStatus.Checked = false;
            }
        }
        else
        {
            txtBasevalue.Text = "";
            chkStatus.Checked = true;
        }
    }

    protected bool CheckAuthCode(string strAuthCode, int intAuthCodeType)
    {
        bool _retValue = false;
        ProviderAuthCode _authcode = new ProviderAuthCode();
        ProviderAuthCodeDB _authcodedb = new ProviderAuthCodeDB();
        _authcode = _authcodedb.GetProviderAuthCode(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()), intAuthCodeType, strAuthCode);
        if (_authcode != null)
        {
            _retValue = true;
        }

        return _retValue;
    }

    protected Int64 InsertProviderAuthTransaction(Int64 TransactionId, string TransactionCode, int TransactionType,
        int AuthCodeType, string AuthCode, Int64 ProviderCompanyId, Int64 RefCEUserId)
    {
        ProviderAuthTransaction _authtrans = new ProviderAuthTransaction();
        ProviderAuthTransactionDB _authtransdb = new ProviderAuthTransactionDB();

        _authtrans.TransactionId = TransactionId;
        _authtrans.TransactionCode = TransactionCode;
        _authtrans.TransactionType = TransactionType;
        _authtrans.AuthCode = AuthCode;
        _authtrans.AuthCodeType = AuthCodeType;
        _authtrans.ProviderCompanyId = ProviderCompanyId;
        _authtrans.RefCEUserID = RefCEUserId;

        _authtrans.AuthTransId = _authtransdb.Insert(_authtrans);
        return _authtrans.AuthTransId;
    }
}
